$(function(){
	
var $list = $("#thelist");

var uploader = WebUploader.create({

    // swf文件路径
    swf: document.getElementsByTagName('base')[0].href + 'resources/js/webuploader/Uploader.swf',

    // 文件接收服务端。
    server: document.getElementsByTagName('base')[0].href + 'secure/upload-uploadify/',

    // 选择文件的按钮。可选。
    // 内部根据当前运行是创建，可能是input元素，也可能是flash.
    pick: '#picker',

    // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
    resize: false
});

//当有文件被添加进队列的时候
uploader.on( 'fileQueued', function( file ) {
    $list.append( '<div id="' + file.id + '" class="item">' +
        '<h4 class="info">' + file.name + '</h4>' +
        '<p class="state">等待上传...</p>' +
    '</div>' );
    
    var a = uploader.upload();
    console.log(a)
});

//文件上传过程中创建进度条实时显示。
uploader.on( 'uploadProgress', function( file, percentage ) {
    var $li = $( '#'+file.id ),
        $percent = $li.find('.progress .progress-bar');

    // 避免重复创建
    if ( !$percent.length ) {
        $percent = $('<div class="progress progress-striped active">' +
          '<div class="progress-bar" role="progressbar" style="width: 0%">' +
          '</div>' +
        '</div>').appendTo( $li ).find('.progress-bar');
    }

    $li.find('p.state').text('上传中');

    $percent.css( 'width', percentage * 100 + '%' );
});

uploader.on( 'uploadSuccess', function( file, response ) {
    $( '#'+file.id ).find('p.state').text('已上传');
    console.log(file);
    console.log(response._raw);
    $("input[name=filePath]").val(response._raw);
});

uploader.on( 'uploadError', function( file ) {
    $( '#'+file.id ).find('p.state').text('上传出错');
});

uploader.on( 'uploadComplete', function( file ) {
    $( '#'+file.id ).find('.progress').fadeOut();
});





});

function doUpload(){
	var filePath = $("input[name=filePath]").val();
	$.ajax({
		headers : {
			'Accept' : 'application/json',
			'Content-Type' : 'application/json'
		},
		type : "POST",
		url : $("#from-question-import").attr("action") + "/" + $(".upload-question-group select").val(),
		data : filePath,
		success : function(message, tst, jqXHR) {
			if (!util.checkSessionOut(jqXHR))
				return false;
			if (message.result == "success") {
				util.success("导入成功", function() {
					$("#submit-div .form-message").text(message.messageInfo);
					//document.location.href = document.getElementsByTagName('base')[0].href + 'admin/course-list';
				});
			} else {
				util.error("操作失败请稍后尝试:" + message.result);
				$("#submit-div .form-message").text(message.messageInfo);
				$("#btn-add-submit").removeAttr("disabled");
			}
		},
		error : function(jqXHR, textStatus) {
			util.error("操作失败请稍后尝试");
			$("#btn-add-submit").removeAttr("disabled");
		}
	});
}


/*$(function(){
	question_import.initial();
});

var question_import={
		initial : function initial() {
			this.prepareUploadify();
			this.questionDataProcess();
			console.log("xxxxxxxxxxxxxxxxxx")
		},
		prepareUploadify : function prepareUploadify(){
			console.log("yyyyyyyyyyyyyyyyyyyyyyyyyy")
			setTimeout(function(){
				console.log("zzzzzzzzzzzzzzz")
				$("#uploadify").uploadify({
			    	'debug'	 : false,
					'buttonText'	: '点击上传附件',
					'buttonCursor'	: 'pointer',
					'uploader'	 : document.getElementsByTagName('base')[0].href + 'secure/upload-uploadify/',
					'queueID': 'fileQueue',
					'swf'	 : document.getElementsByTagName('base')[0].href + 'resources/js/uploadify/uploadify.swf',
					'multi'	 : false,
					'auto'	 : true,
					'height' : '26',
					'width'	 : '160',
					'requeueErrors'	: false,
					'fileSizeLimit'	: '20480', // expects input in kb
					'cancelImage'	: document.getElementsByTagName('base')[0].href + 'resources/js/uploadify/cancel.png',
					removeCompleted : true,
					overrideEvents:['onSelectError','onDialogClose'],
					onUploadComplete: function(file) {
					},
					onUploadSuccess : function(file, data, response) {  
						$('#div-file-list').html('<a class=\'file-name\'>' 
								+ file.name 
								+ '</a><input type=\'hidden\' value=\'' 
								+ data + '\' />'); //+ file.name + '\' />');
			        },
					onSelectError: function(file,errorCode,errorMsg) {
						if(errorCode==-110){
							util.notify("只能上传20M以下的文件。");
							return false;
						}
					},
					onUploadError: function(file,errorCode,errorMsg, errorString) {
						util.error(errorMsg);
					}
			    });
			},2);
		},
		questionDataProcess : function questionDataProcess(){
			$("#from-question-import").submit(function(){
				//var filePath = $("#div-file-list").find("input").val();
				var filePath = $("#div-file-list").find("input").val().split('\\').pop();
				$.ajax({
					headers : {
						'Accept' : 'application/json',
						'Content-Type' : 'application/json'
					},
					type : "POST",
					url : $("#from-question-import").attr("action") + "/" + $(".upload-question-group select").val(),
					data : filePath,
					success : function(message, tst, jqXHR) {
						if (!util.checkSessionOut(jqXHR))
							return false;
						if (message.result == "success") {
							util.success("导入成功", function() {
								$("#submit-div .form-message").text(message.messageInfo);
								//document.location.href = document.getElementsByTagName('base')[0].href + 'admin/course-list';
							});
						} else {
							util.error("操作失败请稍后尝试:" + message.result);
							$("#submit-div .form-message").text(message.messageInfo);
							$("#btn-add-submit").removeAttr("disabled");
						}
					},
					error : function(jqXHR, textStatus) {
						util.error("操作失败请稍后尝试");
						$("#btn-add-submit").removeAttr("disabled");
					}
				});
				return false;
			});
		}
};
*/